package ru.isys.yoomee.ui;

import java.util.ArrayList;
import java.util.List;

import ru.isys.yoomee.R;
import ru.isys.yoomee.model.Yoo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class YoosAllActivity extends Activity {
	public static final String YOOS_ARRAY_KEY="yoos_array_key";
	
	private List<Yoo> mYoos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		Bundle bundle;
		if (( bundle =getIntent().getExtras()) == null) {
			
			setContentView(R.layout.yoos_grid_layout);
			
			
			
			Log.v("FUUUU", "nothing in intent");
			mYoos = new ArrayList<Yoo>();
			mockYoos();

			GridView gridview = (GridView) findViewById(R.id.yoos_grid);
			gridview.setAdapter(new YoosAdapter(this, mYoos));

			gridview.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					// Toast.makeText(HelloGridView.this, "" + position,
					// Toast.LENGTH_SHORT).show();
				}
			});
		}
		else {
			
			setContentView(R.layout.yoos_grid_layout);
			mYoos =(List<Yoo>)bundle.getSerializable(YoosAllActivity.YOOS_ARRAY_KEY);
			
			GridView gridview = (GridView) findViewById(R.id.yoos_grid);
			gridview.setAdapter(new YoosAdapter(this, mYoos));

			gridview.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					// Toast.makeText(HelloGridView.this, "" + position,
					// Toast.LENGTH_SHORT).show();
				}
			});
		}
	}

	private void mockYoos() {
		mYoos.add(new Yoo("Groupon", 12, 31, false));
		mYoos.add(new Yoo("FourSquare"));
		mYoos.add(new Yoo("Facebook"));
		mYoos.add(new Yoo("IBM"));
		mYoos.add(new Yoo("Starbucks"));
		mYoos.add(new Yoo("Apple Store"));
		mYoos.add(new Yoo("Royal Albert Hall"));
		mYoos.add(new Yoo("Mc Donalds"));

	}

	private class YoosAdapter extends BaseAdapter {
		private Context mContext;
		private List<Yoo> mYoos;
		private LayoutInflater mLayoutInflater;
		private TextView mNickname;
		private TextView mDistanceToYoo;
		private TextView mRegisteredAgo;

		public YoosAdapter(Context context, List<Yoo> mYoos) {
			this.mContext = context;
			this.mYoos = mYoos;
		}

		public int getCount() {
			return mYoos.size();
		}

		public Yoo getItem(int position) {
			return this.mYoos.get(position);
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Log.v("TAG", "getView");

			View view;
			if (convertView == null) { // if it's not recycled, initialize some
										// attributes
				mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = mLayoutInflater.inflate(R.layout.yoos_item, null);

			} else {
				view = (View) convertView;
			}

			Yoo yoo = mYoos.get(position);

			if (yoo != null) {
				mNickname = (TextView) view.findViewById(R.id.yoos_item_nickname);
				mDistanceToYoo = (TextView) view.findViewById(R.id.yoos_item_distance_to);
				mRegisteredAgo = (TextView) view.findViewById(R.id.yoos_item_registered_ago);

				if (mNickname != null) {
					mNickname.setText(yoo.getNickName());
				}

				if (mDistanceToYoo != null) {
					mDistanceToYoo.setText(yoo.getDistanceToYouString());
				}
				if (mRegisteredAgo != null) {
					mRegisteredAgo.setText(yoo.getRegisteredAgo());
				}
			}
			return view;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.yoomee_menu_layout, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.mee_config:
			startMeeConfigActivity();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void startMeeConfigActivity() {
		startActivity(new Intent(this, MeeActivity.class));
	}
}
